<template>
  <div
    v-if="visible"
    class="register-modal-overlay"
    @click="handleOverlayClick"
  >
    <div class="register-modal" @click.stop>
      <!-- 关闭按钮 -->
      <div class="close-btn" @click="closeModal">
        <el-icon><Close /></el-icon>
      </div>

      <div class="modal-content">
        <!-- 左侧品牌区域 -->
        <div class="left-section">
          <div class="brand-container">
            <div class="brand-logo">
              <div class="logo-icon">🌟</div>
              <h1 class="brand-title">加入 Trave</h1>
              <p class="brand-subtitle">开启你的旅行故事</p>
            </div>

            <div class="feature-showcase">
              <div class="feature-card">
                <div class="feature-icon">✈️</div>
                <div class="feature-content">
                  <h3>规划旅程</h3>
                  <p>制定完美的旅行计划</p>
                </div>
              </div>
              <div class="feature-card">
                <div class="feature-icon">📸</div>
                <div class="feature-content">
                  <h3>记录美好</h3>
                  <p>分享精彩的旅行瞬间</p>
                </div>
              </div>
              <div class="feature-card">
                <div class="feature-icon">💝</div>
                <div class="feature-content">
                  <h3>收获回忆</h3>
                  <p>珍藏珍贵的旅行回忆</p>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧注册表单 -->
        <div class="right-section">
          <div class="form-container">
            <div class="form-header">
              <h2 class="form-title">创建账号</h2>
              <p class="form-subtitle">加入 Trave 旅行日记，开始你的旅行之旅</p>
            </div>

            <el-form
              ref="formRef"
              :model="registerForm"
              :rules="rules"
              class="register-form"
            >
              <div class="form-group">
                <el-form-item prop="username">
                  <el-input
                    v-model="registerForm.username"
                    placeholder="用户名"
                    size="large"
                    clearable
                    class="custom-input"
                  >
                    <template #prefix>
                      <el-icon class="input-icon"><User /></el-icon>
                    </template>
                  </el-input>
                </el-form-item>

                <el-form-item prop="email">
                  <el-input
                    v-model="registerForm.email"
                    placeholder="邮箱地址"
                    size="large"
                    clearable
                    class="custom-input"
                  >
                    <template #prefix>
                      <el-icon class="input-icon"><Message /></el-icon>
                    </template>
                  </el-input>
                </el-form-item>

                <el-form-item prop="password">
                  <el-input
                    v-model="registerForm.password"
                    type="password"
                    placeholder="密码（6-20位）"
                    size="large"
                    show-password
                    class="custom-input"
                  >
                    <template #prefix>
                      <el-icon class="input-icon"><Lock /></el-icon>
                    </template>
                  </el-input>
                </el-form-item>

                <el-form-item prop="captcha">
                  <div class="captcha-container">
                    <el-input
                      v-model="registerForm.captcha"
                      placeholder="验证码"
                      size="large"
                      maxlength="4"
                      clearable
                      class="captcha-input"
                    >
                      <template #prefix>
                        <el-icon class="input-icon"><CircleCheck /></el-icon>
                      </template>
                    </el-input>
                    <Captcha
                      ref="captchaRef"
                      @update:code="updateCaptcha"
                      :width="120"
                      :height="48"
                      class="captcha-component"
                    />
                  </div>
                </el-form-item>
              </div>

              <el-form-item class="submit-item">
                <el-button
                  type="primary"
                  size="large"
                  :loading="loading"
                  @click="handleRegister"
                  class="register-button"
                >
                  <span v-if="!loading">注册</span>
                  <span v-else>注册中...</span>
                </el-button>
              </el-form-item>
            </el-form>

            <div class="form-footer">
              <div class="login-section">
                <span class="login-text">已有账号？</span>
                <el-button type="text" @click="showLogin" class="login-link">
                  立即登录
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import {
  User,
  Message,
  Lock,
  CircleCheck,
  Close
} from '@element-plus/icons-vue'
import Captcha from './Captcha.vue'
import { register } from '../api/auth'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:visible', 'register-success', 'show-login'])

// 表单数据
const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  captcha: ''
})

// 验证码相关
const captchaRef = ref(null)
const captchaCode = ref('')

// 更新验证码
const updateCaptcha = (code) => {
  captchaCode.value = code
}

// 验证码验证
const validateCaptcha = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入验证码'))
  } else if (value.toLowerCase() !== captchaCode.value.toLowerCase()) {
    callback(new Error('验证码错误'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { validator: validateCaptcha, trigger: 'blur' }
  ]
}

const formRef = ref(null)
const loading = ref(false)

// 关闭弹窗
const closeModal = () => {
  emit('update:visible', false)
}

// 点击遮罩层关闭
const handleOverlayClick = () => {
  closeModal()
}

// 注册处理
const handleRegister = async () => {
  if (!formRef.value) return

  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true

      try {
        // 调用后端注册API
        const result = await register({
          username: registerForm.username,
          email: registerForm.email,
          password: registerForm.password
        })

        if (result.success) {
          ElMessage.success('注册成功！请登录')
          emit('register-success', {
            username: registerForm.username,
            email: registerForm.email
          })
          closeModal()

          // 清空表单
          Object.assign(registerForm, {
            username: '',
            email: '',
            password: '',
            captcha: ''
          })
        } else {
          ElMessage.error(result.message || '注册失败')
          // 注册失败，刷新验证码
          registerForm.captcha = ''
          captchaRef.value?.refreshCaptcha()
        }
      } catch (error) {
        console.error('注册失败:', error)
        // 注册失败，刷新验证码
        registerForm.captcha = ''
        captchaRef.value?.refreshCaptcha()
      }

      loading.value = false
    } else {
      // 验证失败，刷新验证码
      registerForm.captcha = ''
      captchaRef.value?.refreshCaptcha()
    }
  })
}

// 显示登录弹窗
const showLogin = () => {
  emit('show-login')
}
</script>

<style scoped>
/* 遮罩层 */
.register-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  animation: fadeIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    backdrop-filter: blur(0px);
  }
  to {
    opacity: 1;
    backdrop-filter: blur(12px);
  }
}

/* 主弹窗 */
.register-modal {
  width: 90%;
  max-width: 900px;
  background: #ffffff;
  border-radius: 24px;
  box-shadow: 0 32px 64px rgba(0, 0, 0, 0.12), 0 16px 32px rgba(0, 0, 0, 0.08),
    0 0 0 1px rgba(255, 255, 255, 0.05);
  position: relative;
  animation: slideUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(40px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 关闭按钮 */
.close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 10;
  border: 1px solid rgba(0, 0, 0, 0.1);
}

.close-btn:hover {
  background: rgba(0, 0, 0, 0.1);
  transform: scale(1.05);
  border-color: rgba(0, 0, 0, 0.2);
}

/* 内容区域 */
.modal-content {
  display: flex;
  min-height: 600px;
}

/* 左侧品牌区域 */
.left-section {
  flex: 1;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  padding: 60px 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.left-section::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(
    circle,
    rgba(255, 255, 255, 0.1) 0%,
    transparent 70%
  );
  animation: float 8s ease-in-out infinite;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-30px) rotate(180deg);
  }
}

.brand-container {
  text-align: center;
  z-index: 2;
  position: relative;
}

.brand-logo {
  margin-bottom: 50px;
}

.logo-icon {
  font-size: 48px;
  margin-bottom: 16px;
  animation: bounce 2s ease-in-out infinite;
}

@keyframes bounce {
  0%,
  20%,
  50%,
  80%,
  100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-10px);
  }
  60% {
    transform: translateY(-5px);
  }
}

.brand-title {
  font-size: 36px;
  font-weight: 800;
  margin: 0 0 12px 0;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  letter-spacing: -0.5px;
}

.brand-subtitle {
  font-size: 18px;
  font-weight: 400;
  margin: 0;
  opacity: 0.9;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.feature-showcase {
  display: flex;
  flex-direction: column;
  gap: 24px;
  max-width: 300px;
}

.feature-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.feature-card:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.feature-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.feature-content h3 {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.feature-content p {
  margin: 0;
  font-size: 14px;
  opacity: 0.8;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

/* 右侧表单区域 */
.right-section {
  flex: 1;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  width: 100%;
  max-width: 400px;
  padding: 60px 50px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-title {
  font-size: 32px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 8px 0;
  letter-spacing: -0.5px;
}

.form-subtitle {
  font-size: 16px;
  color: #666;
  margin: 0;
  font-weight: 400;
}

/* 表单样式 */
.register-form {
  margin-bottom: 0;
}

.form-group {
  margin-bottom: 32px;
}

.register-form :deep(.el-form-item) {
  margin-bottom: 24px;
}

.register-form :deep(.el-form-item:last-child) {
  margin-bottom: 0;
}

.custom-input :deep(.el-input__wrapper) {
  height: 56px;
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  padding: 0 16px;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #d1d5db;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #f093fb;
  box-shadow: 0 0 0 4px rgba(240, 147, 251, 0.1);
}

.custom-input :deep(.el-input__inner) {
  font-size: 16px;
  color: #1a1a1a;
  font-weight: 400;
}

.input-icon {
  color: #9ca3af;
  font-size: 18px;
}

/* 验证码容器 */
.captcha-container {
  display: flex;
  gap: 12px;
  align-items: center;
}

.captcha-input {
  flex: 1;
}

.captcha-component {
  flex-shrink: 0;
}

/* 注册按钮 */
.submit-item {
  margin-bottom: 0 !important;
}

.register-button {
  width: 100%;
  height: 56px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  border: none;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  letter-spacing: 0.5px;
  box-shadow: 0 4px 12px rgba(240, 147, 251, 0.3);
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(240, 147, 251, 0.4);
}

.register-button:active {
  transform: translateY(0);
}

/* 表单底部 */
.form-footer {
  margin-top: 32px;
}

.login-section {
  text-align: center;
}

.login-text {
  font-size: 14px;
  color: #6b7280;
  margin-right: 8px;
}

.login-link {
  color: #f093fb !important;
  font-weight: 600;
  font-size: 14px;
  padding: 0;
  text-decoration: none;
  transition: color 0.2s ease;
}

.login-link:hover {
  color: #f5576c !important;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .register-modal {
    max-width: 800px;
  }

  .left-section {
    padding: 50px 40px;
  }

  .form-container {
    padding: 50px 40px;
  }
}

@media (max-width: 768px) {
  .register-modal {
    width: 95%;
    max-width: 500px;
  }

  .modal-content {
    flex-direction: column;
    min-height: auto;
  }

  .left-section {
    display: none;
  }

  .form-container {
    padding: 40px 30px;
  }

  .form-title {
    font-size: 28px;
  }
}

@media (max-width: 480px) {
  .form-container {
    padding: 30px 20px;
  }

  .captcha-container {
    flex-direction: column;
    gap: 12px;
  }

  .captcha-input {
    width: 100%;
  }

  .captcha-component {
    width: 100%;
    display: flex;
    justify-content: center;
  }
}
</style>
